/*
 * Project Name: ${module}
 * $Id:  AbstractBaseService.java ${now?string('yyyy-MM-dd HH:mm:ss')} $
 */

package ${basepackage}.service.impl;


import ${basepackage}.dao.IBaseDAO;
import ${basepackage}.domain.BaseDomain;
import ${basepackage}.service.IBaseService;
import ${basepackage}.service.IDaoAware;
import ${basepackage}.common.util.SqlOrder;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * service抽象类，实现基础方法，dao的注入通过子类实现getDao方法
 * @author lxue
 *
 * 2015年10月4日 下午4:41:58
 */
@CacheConfig(cacheNames = { "allunused" })
public abstract class AbstractBaseService<D extends IBaseDAO<T>, T extends BaseDomain>
        implements IBaseService<T>, IDaoAware<D,T> {
    public final int add(T entity) {
        return getDao().insert(entity);
    }

    @CacheEvict(key = "#id")
    public final int delete(Long id) {
        return getDao().deleteById(id);
    }

    @CacheEvict(key = "#entity.id")
    public final int update(T entity) {
        return getDao().update(entity);
    }

    @CacheEvict(key = "#entity.id")
    public final int updateNull(T entity) {
        return getDao().updateNull(entity);
    }

    @CacheEvict(key = "#entity.id")
    public final int deleteById(Long id) {
        return getDao().deleteById(id);
    }

    @CacheEvict(allEntries = true)
    public final int deleteByProperty(String property, Object value) {
        return getDao().deleteByProperty(property, value);
    }

    @Cacheable(key = "#id")
    public final T fetch(Long id) {
        return getDao().fetch(id);
    }

    @Cacheable
    public final T findOne(String property, Object value) {
        return getDao().findOne(property, value, null, null);
    }

    @Cacheable
    public final List<T> findList(String property, Object value) {
        return getDao().findList(property, value, null, null);
    }

    @Cacheable
    public final List<T> findList(String property, Object value, String orderBy,
                                  SqlOrder SqlOrder) {
        return getDao().findList(property, value, orderBy,
                SqlOrder.getAction());
    }

    public final List<T> findAll() {
        return getDao().findAll(null, null);
    }

    public final List<T> findAll(String orderBy, SqlOrder SqlOrder) {
        return getDao().findAll(orderBy, SqlOrder.getAction());
    }

    @Cacheable
    public final List<T> like(Map<String, Object> condition) {
        return getDao().like(condition, null, null);
    }

    @Cacheable
    public final List<T> like(Map<String, Object> condition, String orderBy,
                              SqlOrder SqlOrder) {
        return getDao().like(condition, orderBy, SqlOrder.getAction());
    }

    public final Long selectMaxId() {
        return getDao().selectMaxId();
    }

    @CacheEvict(key = "#id")
    public final void updateOrSave(T entity, Long id) {
        if ((id != null) && (!(StringUtils.isEmpty(id)))) {
            getDao().update(entity);
        } else {
            getDao().insert(entity);
        }
    }

    public final T selectOne(String mapperId, Object obj) {
        return getDao().selectOne(mapperId, obj);
    }

    public final List<T> selectList(String mapperId, Object obj) {
        return getDao().selectList(mapperId, obj);
    }

    public final int count(Map<String,Object> condition) {
        return getDao().count(condition);
    }

    public final int countAll(){
        return getDao().count(null);
    }

    public final T queryOne(Map<String,Object> condition) {
        return getDao().queryOne(condition, null, null);
    }

    public final T queryOne(Map<String,Object> condition, String orderBy,
                            SqlOrder SqlOrder) {
        return getDao().queryOne(condition, orderBy, SqlOrder.getAction());
    }

    public final List<T> queryList(Map<String,Object> condition, String orderBy, SqlOrder SqlOrder) {
        return getDao().queryList(condition, orderBy, SqlOrder.getAction());
    }

    public final List<T> queryList(Map<String,Object> condition) {
        return getDao().queryList(condition, null, null);
    }

    @CacheEvict(allEntries = true)
    public final int deleteByCondition(Map<String,Object> condition) {
        return getDao().deleteByCondition(condition);
    }

    @CacheEvict(allEntries = true)
    public final int updateMap(Map<String,Object> entityMap) {
        return getDao().updateMap(entityMap);
    }

    public final int insertMap(Map<String,Object> entityMap) {
        return getDao().insertMap(entityMap);
    }

    public final List<T> queryListByPage(Map<String,Object> condition, int offset, int rows) {
        return getDao().queryPage(condition, offset, rows, null, null);
    }

    public final List<T> queryListByPage(Map<String,Object> condition, int offset, int rows,
                                         String orderBy, SqlOrder SqlOrder) {
        return getDao().queryPage(condition, offset, rows, orderBy,
                SqlOrder.getAction());
    }

}
